#include <rtthread.h>
#include <rtdevice.h>
#include "functionlib.h"

#define NULL ((void *)0)


//小功能函数库
//数组转换为UINT32类型(大端字节数组，高字节在低地址)，转换为UINT32类型
uint32_t BufToU32(uint8_t* in, uint8_t len)
{
    uint32_t value = 0;
    uint8_t i;

    if ((in == NULL) || (len > 4))
    {
        return 0;
    }

    for (i = 0; i < len; i++)
    {
        value = value << 8;

        value |= in[i];

    }

    return value;

}

//数组转换为UINT16类型(大端字节数组，高字节在低地址)，转换为UINT16类型

uint16_t BufToU16(uint8_t* in)
{
    uint16_t value = 0;
    uint8_t i;

    if (in == NULL)
    {
        return 0;
    }

    for (i = 0; i < 2; i++)
    {
        value = value << 8;

        value |= in[i];

    }

    return value;

}

//数组转换为UINT32类型(大端字节数组，高字节在低地址)，转换为UINT32类型
int32_t BufToUint(uint8_t* in, uint8_t len)
{
    int32_t value = 0;
    uint8_t i;

    if (in == NULL)
    {
        return -1;
    }

    for (i = 0; i < len; i++)
    {
        value = value << 8;

        value |= in[i];

    }

    return value;

}



//U16转换为字符串(大端字节数组，高字节在低地址)

void U16ToBuf(uint16_t in, uint8_t* out)
{
    uint8_t i = 0;

    for (i = 0; i < 2; i++)
    {
        out[i] = (in >> 8 * (1 - i)) & 0xff;

    }
}

//U32转换为字符串(大端字节数组，高字节在低地址)

void U32ToBuf(uint32_t in, uint8_t* out)
{
    uint8_t i = 0;

    for (i = 0; i < 4; i++)
    {
        out[i] = (in >> 8 * (3 - i)) & 0xff;

    }

}


/**
 * @brief       将INT格式转换为BCD格式，小端模式
 * @param
                BCD格式输出，高位在高地址(小端模式)

 * @out         地址BCD码
 * @par         创建
 *              lcj于2020-7-2创建
 */

void int_to_bcd(uint32_t intvalue, uint8_t* bcddata, uint8_t bcdlen)
{
    uint8_t i = 0;

    for (i = 0; i < bcdlen; i++)
    {
        bcddata[i] = (intvalue >> (i * 8)) & 0x000000ff;
    }
}

/**
 * @brief       将BCD格式转换为int格式
 * @param
                bcd格式输入，高位在高地址(小端模式)

 * @out         地址BCD码
 * @par         创建
 *              lcj于2020-7-2创建
 */

void bcd_to_int(uint32_t* intvalue, uint8_t* bcddata, uint8_t bcdlen)
{
    uint8_t i = 0;

    for (i = 0; i < bcdlen; i++)
    {
        *intvalue |= (bcddata[i] << (i * 8));
    }
}

void ascii_2_hex(uint8_t* dst, uint8_t* src, uint32_t len)
{
    uint32_t offset = 0;

    while (offset < len)
    {
        uint8_t temp = 0;
        uint8_t index = 0;

        if ((len - offset) == 1)
        {
            break;
        }

        for (index = 0; index < 2; index++)
        {
            uint8_t data = src[offset + index];
            if ((data >= '0') && (data <= '9'))
            {
                data -= '0';
            }
            else if ((data >= 'a') && (data <= 'f'))
            {
                data -= 'a';
                data += 10;
            }
            else if ((data >= 'A') && (data <= 'F'))
            {
                data -= 'A';
                data += 10;
            }
            temp |= data << (4 * (1 - index));
        }
        *dst++ = temp;
        offset += 2;
    }
}

void hex_2_ascii(uint8_t* dst, uint8_t* src, uint32_t len)
{
    uint8_t temp;

    while (len--)
    {
        temp = *src++;
        if ((temp >> 4) > 9)
        {
            *dst++ = (temp >> 4) + 0x37;
        }
        else
        {
            *dst++ = (temp >> 4) + 0x30;
        }

        if ((temp & 0xF) > 9)
        {
            *dst++ = (temp & 0xF) + 0x37;
        }
        else
        {
            *dst++ = (temp & 0xF) + 0x30;
        }
    }

    *dst = 0;
}

void ascii_2_int(uint8_t* dst, uint8_t* src, uint32_t len)
{
    uint32_t offset = 0;

    while (offset < len)
    {
        uint8_t temp = 0;
        uint8_t index = 0;

        if ((len - offset) == 1)
        {
            break;
        }

        for (index = 0; index < 2; index++)
        {
            uint8_t data = src[offset + index];
            if ((data >= '0') && (data <= '9'))
            {
                data -= '0';
            }
            else if ((data >= 'a') && (data <= 'f'))
            {
                data -= 'a';
                data += 10;
            }
            else if ((data >= 'A') && (data <= 'F'))
            {
                data -= 'A';
                data += 10;
            }
            temp *= 10 * index;
            temp += data;
        }
        *dst++ = temp;
        offset += 2;
    }
}


